package neusoft.example.com.yuejian06.Adapter;

import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.ClubvideoDetailActivity;
import neusoft.example.com.yuejian06.Bean.Clubvideo;
import neusoft.example.com.yuejian06.PublicIp;
import neusoft.example.com.yuejian06.R;

public class ClubvideoListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //private LayoutInflater inflater;
    private Context context;
    private List<Clubvideo> list;

    public ClubvideoListAdapter(Context context,List<Clubvideo> list){
        this.list=list;
        this.context=context;
    }

    public void setList(List<Clubvideo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=LayoutInflater.from(context).inflate(R.layout.adapter_clubvideolist,parent,false);
        RecyclerView.ViewHolder viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final Clubvideo bean=list.get(position);
        if(bean==null) return;
        final ClubvideoListAdapter.ViewHolder viewHolder=(ClubvideoListAdapter.ViewHolder) holder;

        viewHolder.clubvideo_name.setText(bean.getClubvideo_name());
        viewHolder.clubvideo_time.setText(bean.getClubvideo_time());
        viewHolder.clubvideo_intro.setText(bean.getClubvideo_intro());

        viewHolder.clubvideo_video.setMediaController(new MediaController(context));
        viewHolder.clubvideo_video.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mediaPlayer) {
                Toast.makeText(context,"播放完成了",Toast.LENGTH_LONG).show();
            }
        });
        viewHolder.clubvideo_video.setVideoPath(PublicIp.ip+"static/videos/"+bean.getClubvideo_video());
        viewHolder.clubvideo_video.requestFocus();
        viewHolder.clubvideo_video.start();

        /*viewHolder.clubvideo_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                viewHolder.clubvideo_video.start();
            }
        });*/
       /* Picasso.with(context)
                .load(publicActivity.getIp()+"static/club_picture/"+bean.getClubvideo_video())
                .into(viewHolder.clubvideo_video);*/
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context,ClubvideoDetailActivity.class);
                intent.putExtra("clubvideo_id", bean.getClubvideo_id());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
       VideoView clubvideo_video;
        TextView clubvideo_name,clubvideo_time,clubvideo_intro;
        public ViewHolder(View itemView) {
            super(itemView);
           clubvideo_video=(VideoView) itemView.findViewById(R.id.clubvideo_video);
            clubvideo_name=(TextView) itemView.findViewById(R.id.clubvideo_name);
            clubvideo_time=(TextView) itemView.findViewById(R.id.clubvideo_time);
            clubvideo_intro=(TextView) itemView.findViewById(R.id.clubvideo_intro);
        }
    }
}

